package leetcode.code1705;

import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int eatenApples(int[] apples, int[] days) {
		int ans = 0;
		PriorityQueue<int[]> p = new PriorityQueue<>((a, b) -> a[0] - b[0]);
		int day = 0;
		int len = apples.length;
		while (day < len) {
			if (apples[day] > 0) {// 装苹果
				p.add(new int[] { day + days[day], apples[day] });
			}
			while (!p.isEmpty()) {// 翻找苹果吃
				int[] left = p.poll();
				if (day < left[0]) {
					ans += 1;
					if (left[1] - 1 > 0) {
						p.add(new int[] { left[0], left[1] - 1 });
					}
					break;// 吃着一个就拉倒
				}
			}
			day++;
		}
		while (!p.isEmpty()) {// 没得装了，但是可能有的吃
			int[] left = p.poll();
			if (day < left[0]) {
				ans += 1;
				if (left[1] - 1 > 0) {
					p.add(new int[] { left[0], left[1] - 1 });
				}
				day++;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(4, this.eatenApples(HelpDebug.str2array3("[2,1,10]"), HelpDebug.str2array3("[2,10,1]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(7, this.eatenApples(HelpDebug.str2array3("[1,2,3,5,2]"), HelpDebug.str2array3("[3,2,1,4,2]")));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(8, this.eatenApples(HelpDebug.str2array3("[2,1,1,4,5]"), HelpDebug.str2array3("[10,10,6,4,2]")));
	}

	@Override
	public void debug4() {
		HelpDebug.compare(0, this.eatenApples(HelpDebug.str2array3("[0]"), HelpDebug.str2array3("[1]")));

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
